// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../Commons/GameEvent";
import { loadjsScript } from "../GlobalFunction";
import BasePlatform from "./BasePlatform";
import PlatformManager from "./PlatformManager";

const { ccclass, property } = cc._decorator;

export const GAME_ID = 1;

@ccclass
export default class PBPlatform extends BasePlatform {

    initSdk() {
        let url = "http://pbsdk.game4.cn/PBSDK-1.0.0.min.js";
        //loadjsScript(url, this.loadScriptComplete);
        let script = document.createElement("script");
        script.src = url;
        script.type = "text/javascript";
        script.onload = this.loadScriptComplete;
        document.getElementsByTagName("head")[0].appendChild(script);
    }

    async loadScriptComplete() {
        let tsdk = window["PBSDK"];
        PlatformManager.single.basePlatform.sdk = tsdk;
        let result = await PlatformManager.single.basePlatform.sdk.init({
            // 开启调试模式 版本支持 默认false
            debug: false,
            // appId 由服务端分配
            appId: 'b50ef211e8311e1130827d2f2558d57b',
            // 游戏ID 由服务端分配
            gameId: GAME_ID,
            // 游戏名称 由服务端分配
            gameName: '小小契约',
            features: [
                // 注册
                "register",
                // 登录
                "login",
                // 防沉迷
                "fcm",
                // 退出登录
                "logout",
                // 支付金额上报
                "payAmount",
                // 敏感词过滤
                "sensitiveWordFilter",
                // 游戏数据上报
                "gameDataReport",
                // 获取游戏数据
                "getGameData",
                //排行榜
                "uploadRank",
                "sendChatMessage",
                "getChatMessage"
            ],
        });
        if (result.code == 200) {
            GameEvent.eventNode.emit(GameEvent.LOADSDKCOMPLETE_EVENT);
        } else {

        }
    }
}
